/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     gesture.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/

#include "gesture.h"
#include "data.h"
#include "stdlib.h"

#include "popupBox.h"
#include "levetop.h"
#include "uart.h"
#include "function_drv.h"

extern uint8_t Basci_flag;
extern uint8_t Adj_flag;
extern uint8_t Slide_flag;
extern uint8_t Dio_flag;
extern uint8_t AioKb_flag;
extern uint8_t GBKioKb_flag;
extern uint8_t Kb_flag;
extern uint8_t VarKey_flag;
extern uint8_t Pop_up_flag;
extern TP_INFO gTpInfo;
extern uint8_t button_Press_flag;



#define GESTURE_THRESHOLD 50	  // Gesture effective sliding threshold
uint8_t Gesture_press = 0;		  // Gesture effective press/release
uint8_t Gesture_direction = 0xFF; // Gesture direction
uint8_t Gesture_flag = 0;		  // Gesture function on/off
/*sliding gesture*/
Gesture_Info gGesture_Info;
/***********************************************************************************
 * Function Name : Gesture_touch
 * Description   : Touch detection of gesture.
 * Input         : None
 * Output        : None
 * Return        : None
 ************************************************************************************/
int Menu_1_flag=1;
int Menu_2_flag=1;
int Set_flag=1;
uint8_t Highlighted_state=1;
uint8_t Page_sum=10;
uint16_t Page_sum1=1;
uint8_t File_countA=1;
uint16_t File_countB=10;
int File_level=1;
uint16_t set_Filelevel=10;
static const int varAddr[] = {
    0x0AB1*2+1,
    0x0AB2*2+1,
    0x0AB3*2+1,
    0x0AB4*2+1,
    0x0AB5*2+1
};

void Gesture_touch(void)
{
	uint8_t i = 0;
	int16_t Gesture_X, Gesture_Y;
	uint8_t rebuf[20];

	// press down
//	if (gTpInfo.sta && button_Press_flag==0)
	if (gTpInfo.sta && Gesture_press==0)
	{
		// sustain
		Gesture_X = gTpInfo.x[0] - First_pressX;
		Gesture_Y = gTpInfo.y[0] - First_pressY;
		if (Gesture_X >= 25 || Gesture_X <= -25 || Gesture_Y >= 25 || Gesture_Y <= -25)
		{
			Gesture_press = 1;

			// cancel
			Basci_flag = 0;
			Adj_flag = 0;
			Slide_flag = 0;
			Dio_flag = 0;
			AioKb_flag = 0;
			GBKioKb_flag = 0;
			Kb_flag = 0;
//			sldmenu_flag = 0;
			VarKey_flag = 0;
			Pop_up_flag = 0;
//			button_Press_flag = 1;
		}
	}


	// raise
	if (gTpInfo.sta == 0 && Gesture_press == 1)
	{
		Gesture_press = 0;
		button_Press_flag = 0;
		Gesture_X = gTpInfo.x[0] - First_pressX;
		Gesture_Y = gTpInfo.y[0] - First_pressY;
		if (Gesture_X >= GESTURE_THRESHOLD || Gesture_X <= -GESTURE_THRESHOLD || Gesture_Y >= GESTURE_THRESHOLD || Gesture_Y <= -GESTURE_THRESHOLD)
		{
			if (abs(Gesture_Y) > abs(Gesture_X))
			{
				if (Gesture_Y < 0)
				{
					Gesture_direction = 0; // up
				}
				else
					Gesture_direction = 1; // down
			}
			else
			{
				if (Gesture_X < 0)
				{
					Gesture_direction = 2; // left
				}
				else
					Gesture_direction = 3; // right
			}
		}

		if(Gesture_direction==0){
			Icon_flag=0;
			var[0x0008 * 2 + 1]=Icon_flag;
		    if(pic_id==1){
			Menu_1_flag++;
			 if(Menu_1_flag>5)
				Menu_1_flag=5;
				var[0x0100*2+1] = Menu_1_flag;
		    }
			if(pic_id==2){
			Menu_2_flag++;
			 if(Menu_2_flag>9)
				 Menu_2_flag=9;
				var[0x0200*2+1] = Menu_2_flag;
				if (Highlighted_state >= 1 && Highlighted_state <= 9 && Menu_2_flag >= 1 && Menu_2_flag <= 9) {
				    int index = Highlighted_state + 2 - Menu_2_flag;
				    for (int i = 0; i < 5; i++) {
				        var[varAddr[i]] = 0;
				    }
				    if (index >= 0 && index < 5) {
				        var[varAddr[index]] = 1;
				    }
				}
			}
			if(pic_id==7){
				Set_flag++;
			 if(Set_flag>2)
				 Set_flag=2;
				var[0x07BB*2+1] = Set_flag;
			}
			if(pic_id==10||pic_id==11||pic_id==12){
				Page_sum++;
			 if(Page_sum>12)
				 Page_sum=12;
			    Display_page(Page_sum);
			}
			if (pic_id > 12 && pic_id < 18) {
			    Page_sum1++;
			    if (Page_sum1 > File_countB) {
			        Page_sum1 = File_countB;
			    }
			    send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
			    if (File_countB >= 5) {
			        int target_page;
			        if (Page_sum1 == 1) {
			            target_page = 13;
			        } else if (Page_sum1 == 2) {
			            target_page = 14;
			        } else if (Page_sum1 >= 3 && Page_sum1 <= File_countB - 2) {
			            target_page = 15;
			        } else if (Page_sum1 == File_countB - 1) {
			            target_page = 16;
			        } else if (Page_sum1 == File_countB) {
			            target_page = 17;
			        }
			        Display_page(target_page);
			    }
			}
		}
		if(Gesture_direction==1){
			Icon_flag=0;
			var[0x0008 * 2 + 1]=Icon_flag;
		    if(pic_id==1){
			 Menu_1_flag--;
			 if(Menu_1_flag<1)
				Menu_1_flag=1;
				var[0x0100*2+1] = Menu_1_flag;
		    }
			if(pic_id==2){
				Menu_2_flag--;
			 if(Menu_2_flag<1)
				 Menu_2_flag=1;
				var[0x0200*2+1] = Menu_2_flag;
				if (Highlighted_state >= 1 && Highlighted_state <= 9 && Menu_2_flag >= 1 && Menu_2_flag <= 9) {
				    int index = Highlighted_state + 2 - Menu_2_flag;
				    for (int i = 0; i < 5; i++) {
				        var[varAddr[i]] = 0;
				    }
				    if (index >= 0 && index < 5) {
				        var[varAddr[index]] = 1;
				    }
				}
			}
			if(pic_id==7){
				Set_flag--;
			 if(Set_flag<1)
				 Set_flag=1;
				var[0x07BB*2+1] = Set_flag;
			}
			if(pic_id==10||pic_id==11||pic_id==12){
				Page_sum--;
			 if(Page_sum<10)
				 Page_sum=10;
			    Display_page(Page_sum);
			}
			if (pic_id > 12 && pic_id < 18) {
			    Page_sum1--;
			    if (Page_sum1 < 1) {
			        Page_sum1 = 1;
			    }
			    send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
			    if (File_countB >= 5) {
			        int target_page;
			        if (Page_sum1 == 1) {
			            target_page = 13;
			        } else if (Page_sum1 == 2) {
			            target_page = 14;
			        } else if (Page_sum1 >= 3 && Page_sum1 <= File_countB - 2) {
			            target_page = 15;
			        } else if (Page_sum1 == File_countB - 1) {
			            target_page = 16;
			        } else if (Page_sum1 == File_countB) {
			            target_page = 17;
			        }
			        Display_page(target_page);
			    }
			}
		}
		if(Gesture_direction==3){
		    if(pic_id==2||pic_id==4||pic_id==6||pic_id==7||pic_id==10||pic_id==11||pic_id==12){
		    	Display_page(1);
		    }
		    else if(pic_id==5){
			  if(Usb_flag==1){
			   Display_page(4);
			  }
			  else
			   Display_page(6);
		    }
		    else if(pic_id==8||pic_id==9){
			   Display_page(7);
		    }
			else if(pic_id > 12 && pic_id < 18){
				File_level--;
			    if (File_level <0)
			    	File_level = 0;
				    Page_sum1=1;
					if(File_level>0){Display_page(13);}
					else if(File_level==0){Display_page(1);}
					var[0x0EA0*2+1] = File_level;
					send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
				 for (int i = 0; i < 5; i++) {
				     int addr = 0x1011 + i * 0x20;
				     var[addr * 2] = 0;
				     var[addr * 2 + 1] = 0;
				 }
			}
		}

		/*if (Gesture_direction != 0xFF)
		{
			if (gGesture_Info.page[Gesture_direction] != 0xFFFF)
			{
				Gesture_flag = 0;
				if (gGesture_Info.Code == 0xC000)
				{
					rebuf[3] = 0x41;
					rebuf[4] = 0xFF;
					rebuf[5] = 0xFF;
					rebuf[6] = (gGesture_Info.value[Gesture_direction] >> 8) & 0xFF;
					rebuf[7] = gGesture_Info.value[Gesture_direction] & 0xFF;
					LT_SendData_CRC_Frame(rebuf, 5);
				}
				Display_page(gGesture_Info.page[Gesture_direction]);
			}

			Gesture_direction = 0xFF;
		}*/
	}
}




uint8_t gFirstTouch = 0;
uint8_t Ges_sc_flag = 0;
uint16_t GeslastXY = 0;
uint16_t Gespos = 0;
uint16_t last_Gespos = 0;
/****Slide screen****/
Sld_sc_Info gSld_sc_Info;
/**********/
#include "css_psram.h"
void slide_screen(void)
{
	uint8_t rebuf[20];
	uint16_t i;
	uint16_t pos;
	int16_t df = 0;


	if (gTpInfo.sta)
	{
		if (gFirstTouch == 0) // First press
		{
			if (Screen_DIR == 0 || Screen_DIR == 2)
			{
				Gespos = LCD_XSIZE_TFT;
				last_Gespos = LCD_XSIZE_TFT;
				if (GeslastXY == 0)
					GeslastXY = gTpInfo.x[0];

				df = GeslastXY - gTpInfo.x[0];
			}
			else if (Screen_DIR == 3 || Screen_DIR == 1)
			{
				Gespos = LCD_YSIZE_TFT;
				last_Gespos = LCD_YSIZE_TFT;
				if (GeslastXY == 0)
					GeslastXY = gTpInfo.y[0];

				df = GeslastXY - gTpInfo.y[0];

			}
			if ((df > 25) || (df < -25))
			{
				gFirstTouch = 1;
				button_Press_flag = 1;
				Basci_flag = 0;
				Adj_flag = 0;
				Slide_flag = 0;
				Dio_flag = 0;
				AioKb_flag = 0;
				Kb_flag = 0;
				VarKey_flag = 0;
				Pop_up_flag = 0;
				//sldmenu_flag = 0;
				GBKioKb_flag = 0;
				Pop_up_press = 0;
			}


			if (Screen_DIR == 0 || Screen_DIR == 2)
			{
				for(i = gSld_sc_Info.ys; i <= gSld_sc_Info.ye; i++)
				{
					uint8_t* addr1 = (uint8_t *)(RAM_display_addr + LCD_XSIZE_TFT * i * 2);
					LT_CSS_PSRAM_Write_QPI_DMA(LCD_XSIZE_TFT * LCD_YSIZE_TFT * 2  + LCD_XSIZE_TFT * 3  * 2 * i + LCD_XSIZE_TFT * 2, addr1, LCD_XSIZE_TFT*2);
				}
			}
			else if (Screen_DIR == 1 || Screen_DIR == 3)
			{
				for(i = 0; i < LCD_YSIZE_TFT; i++)
				{
					uint8_t* addr1 = (uint8_t *)(RAM_display_addr + gSld_sc_Info.ys * 2 + LCD_XSIZE_TFT * i * 2);
					LT_CSS_PSRAM_Write_QPI_DMA(LCD_XSIZE_TFT * LCD_YSIZE_TFT * 2  + gSld_sc_Info.ys * 2 + LCD_XSIZE_TFT * 2 * (LCD_YSIZE_TFT + i), addr1, (gSld_sc_Info.ye - gSld_sc_Info.ys + 1)*2);
				}
			}


		}

		else
		{
			if (Screen_DIR == 0 || Screen_DIR == 2)
			{
				df = GeslastXY - gTpInfo.x[0];

				if (df != 0)
				{
					GeslastXY = gTpInfo.x[0];
					Gespos += df;

					if (gSld_sc_Info.page[0] == 0xFFFF)
					{
						if (Gespos > LCD_XSIZE_TFT)
							Gespos = LCD_XSIZE_TFT;
					}
					if (gSld_sc_Info.page[1] == 0xFFFF)
					{
						if (Gespos < LCD_XSIZE_TFT)
							Gespos = LCD_XSIZE_TFT;
					}
					pos = Gespos - Gespos % 4;
					if (pos != last_Gespos)
					{
						LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
											Gespos,gSld_sc_Info.ys,LCD_XSIZE_TFT * 3,
											0,gSld_sc_Info.ys,LCD_XSIZE_TFT - 1,gSld_sc_Info.ye - gSld_sc_Info.ys + 1);

					}

					last_Gespos = pos;
				}
			}
			else if (Screen_DIR == 1 || Screen_DIR == 3)
			{
				df = GeslastXY - gTpInfo.y[0];

				if (df != 0)
				{
					GeslastXY = gTpInfo.y[0];
					Gespos += df;

					if (gSld_sc_Info.page[0] == 0xFFFF)
					{
						if (Gespos > LCD_YSIZE_TFT)
							Gespos = LCD_YSIZE_TFT;
					}
					if (gSld_sc_Info.page[1] == 0xFFFF)
					{
						if (Gespos < LCD_YSIZE_TFT)
							Gespos = LCD_YSIZE_TFT;
					}
					pos = Gespos - Gespos % 4;
					if (pos != last_Gespos)
					{
						LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT *2,
											gSld_sc_Info.ys,Gespos,LCD_XSIZE_TFT,
											gSld_sc_Info.ys,0,gSld_sc_Info.ye - gSld_sc_Info.ys + 1,LCD_YSIZE_TFT);

					}

					last_Gespos = pos;
				}
			}

		}
	}
	else if (gFirstTouch == 1)
	{
		gFirstTouch = 0;
		Basci_flag = 0;
		Adj_flag = 0;
		Slide_flag = 0;
		Dio_flag = 0;
		AioKb_flag = 0;
		GBKioKb_flag = 0;
		Kb_flag = 0;
		//sldmenu_flag = 0;
		VarKey_flag = 0;
		Pop_up_flag = 0;
		button_Press_flag = 0;

		if (Screen_DIR == 0 || Screen_DIR == 2)
		{

			if (Gespos > (LCD_XSIZE_TFT + LCD_XSIZE_TFT * 3 / 10)) // Turn the page
			{

				for (i = 0; i < LCD_XSIZE_TFT * 2 - last_Gespos; i ++)
				{
					if(LCD_XSIZE_TFT * 2 - last_Gespos - i > 200)
						i += 40;
					else if(LCD_XSIZE_TFT * 2 - last_Gespos - i > 100)
						i += 20;
//					else if(LCD_XSIZE_TFT * 2 - last_Gespos - i > 40)
//						i += 8;
					else if(LCD_XSIZE_TFT * 2 - last_Gespos - i > 20)
						i += 8;
					else if(LCD_XSIZE_TFT * 2 - last_Gespos - i <= 8)
						i =  LCD_XSIZE_TFT * 2 - last_Gespos;
					LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
										last_Gespos + i,gSld_sc_Info.ys,LCD_XSIZE_TFT * 3,
										0,gSld_sc_Info.ys,LCD_XSIZE_TFT - 1,gSld_sc_Info.ye - gSld_sc_Info.ys + 1);
				}


//				LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
//									LCD_XSIZE_TFT,0,LCD_XSIZE_TFT * 3,
//									0,0,LCD_XSIZE_TFT,LCD_YSIZE_TFT);


				if (gSld_sc_Info.Code == 0xC00C)
				{
					rebuf[3] = 0x41;
					rebuf[4] = 0xFF;
					rebuf[5] = 0xFF;
					rebuf[6] = (gSld_sc_Info.value[0] >> 8) & 0xFF;
					rebuf[7] = gSld_sc_Info.value[0] & 0xFF;
					LT_SendData_CRC_Frame(rebuf, 5);
				}

				Display_page(gSld_sc_Info.page[0]);
				GeslastXY = 0;
			}

			else if (Gespos < LCD_XSIZE_TFT * 7 / 10) 				// Turn the page
			{
				for (i = 0; i < last_Gespos; i ++)
				{
					if(last_Gespos - i > 200)
						i += 40;
					else if(last_Gespos - i > 100)
						i += 20;
	//				else if(last_Gespos - i > 40)
	//					i += 8;
					else if(last_Gespos - i > 20)
						i += 8;
					else if(last_Gespos - i <= 8)
						i = last_Gespos;
					LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
										last_Gespos - i,gSld_sc_Info.ys,LCD_XSIZE_TFT * 3,
										0,gSld_sc_Info.ys,LCD_XSIZE_TFT - 1,gSld_sc_Info.ye - gSld_sc_Info.ys + 1);
				}

	//			LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
	//								0,0,LCD_XSIZE_TFT * 3,
	//								0,0,LCD_XSIZE_TFT,LCD_YSIZE_TFT);


				if (gSld_sc_Info.Code == 0xC00C)
				{
					rebuf[3] = 0x41;
					rebuf[4] = 0xFF;
					rebuf[5] = 0xFF;
					rebuf[6] = (gSld_sc_Info.value[0] >> 8) & 0xFF;
					rebuf[7] = gSld_sc_Info.value[0] & 0xFF;
					LT_SendData_CRC_Frame(rebuf, 5);
				}

				Display_page(gSld_sc_Info.page[1]);
				GeslastXY = 0;

			}
			else if (last_Gespos != LCD_XSIZE_TFT)
			{

				if (last_Gespos > LCD_XSIZE_TFT)
				{

					for (i = 0; i < last_Gespos - LCD_XSIZE_TFT; i += 4)
					{
						LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT *2,
											last_Gespos - i,gSld_sc_Info.ys,LCD_XSIZE_TFT * 3,
											0,gSld_sc_Info.ys,LCD_XSIZE_TFT - 1,gSld_sc_Info.ye - gSld_sc_Info.ys + 1);
					}

					LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT *2,
										LCD_XSIZE_TFT,gSld_sc_Info.ys,LCD_XSIZE_TFT * 3,
										0,gSld_sc_Info.ys,LCD_XSIZE_TFT - 1,gSld_sc_Info.ye - gSld_sc_Info.ys + 1);

					GeslastXY = 0;
					Gespos = LCD_XSIZE_TFT;

				}
				else
				{
					for (i = 0; i < LCD_XSIZE_TFT - last_Gespos; i += 4)
					{
						LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT *2,
											last_Gespos + i,gSld_sc_Info.ys,LCD_XSIZE_TFT * 3,
											0,gSld_sc_Info.ys,LCD_XSIZE_TFT - 1,gSld_sc_Info.ye - gSld_sc_Info.ys + 1);
					}
					LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT * 2,
										LCD_XSIZE_TFT,gSld_sc_Info.ys,LCD_XSIZE_TFT * 3,
										0,gSld_sc_Info.ys,LCD_XSIZE_TFT - 1,gSld_sc_Info.ye - gSld_sc_Info.ys + 1);

					GeslastXY = 0;
					Gespos = LCD_XSIZE_TFT;

				}
			}
		}
		else if (Screen_DIR == 1 || Screen_DIR == 3)
		{
			if (Gespos > (LCD_YSIZE_TFT + LCD_YSIZE_TFT * 3 / 10)) // Turn the page
			{
				for (i = 0; i < LCD_YSIZE_TFT * 2 - last_Gespos; i ++)
				{
					if(LCD_YSIZE_TFT * 2 - last_Gespos - i > 200)
						i += 40;
					else if(LCD_YSIZE_TFT * 2 - last_Gespos - i > 100)
						i += 20;
//					else if(LCD_XSIZE_TFT * 2 - last_Gespos - i > 40)
//						i += 8;
					else if(LCD_YSIZE_TFT * 2 - last_Gespos - i > 20)
						i += 8;
					else if(LCD_YSIZE_TFT * 2 - last_Gespos - i <= 8)
						i =  LCD_YSIZE_TFT * 2 - last_Gespos;
					LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
										gSld_sc_Info.ys,last_Gespos + i,LCD_XSIZE_TFT,
										gSld_sc_Info.ys,0,gSld_sc_Info.ye - gSld_sc_Info.ys + 1,LCD_YSIZE_TFT );
				}


//				LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
//									LCD_XSIZE_TFT,0,LCD_XSIZE_TFT * 3,
//									0,0,LCD_XSIZE_TFT,LCD_YSIZE_TFT);


				if (gSld_sc_Info.Code == 0xC00C)
				{
					rebuf[3] = 0x41;
					rebuf[4] = 0xFF;
					rebuf[5] = 0xFF;
					rebuf[6] = (gSld_sc_Info.value[0] >> 8) & 0xFF;
					rebuf[7] = gSld_sc_Info.value[0] & 0xFF;
					LT_SendData_CRC_Frame(rebuf, 5);
				}

				Display_page(gSld_sc_Info.page[0]);
				GeslastXY = 0;
			}

			else if (Gespos < LCD_YSIZE_TFT * 7 / 10) 				// Turn the page
			{
				for (i = 0; i < last_Gespos; i ++)
				{
					if(last_Gespos - i > 200)
						i += 40;
					else if(last_Gespos - i > 100)
						i += 20;
	//				else if(last_Gespos - i > 40)
	//					i += 8;
					else if(last_Gespos - i > 20)
						i += 8;
					else if(last_Gespos - i <= 8)
						i = last_Gespos;
					LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
										gSld_sc_Info.ys,last_Gespos - i,LCD_XSIZE_TFT,
										gSld_sc_Info.ys,0,gSld_sc_Info.ye - gSld_sc_Info.ys + 1,LCD_YSIZE_TFT );
				}

	//			LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT* LCD_YSIZE_TFT *2,
	//								0,0,LCD_XSIZE_TFT * 3,
	//								0,0,LCD_XSIZE_TFT,LCD_YSIZE_TFT);


				if (gSld_sc_Info.Code == 0xC00C)
				{
					rebuf[3] = 0x41;
					rebuf[4] = 0xFF;
					rebuf[5] = 0xFF;
					rebuf[6] = (gSld_sc_Info.value[0] >> 8) & 0xFF;
					rebuf[7] = gSld_sc_Info.value[0] & 0xFF;
					LT_SendData_CRC_Frame(rebuf, 5);
				}

				Display_page(gSld_sc_Info.page[1]);
				GeslastXY = 0;

			}
			else if (last_Gespos != LCD_YSIZE_TFT)
			{

				if (last_Gespos > LCD_YSIZE_TFT)
				{

					for (i = 0; i < last_Gespos - LCD_YSIZE_TFT; i += 4)
					{
						LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT *2,
											gSld_sc_Info.ys,last_Gespos - i,LCD_XSIZE_TFT,
											gSld_sc_Info.ys,0,gSld_sc_Info.ye - gSld_sc_Info.ys + 1,LCD_YSIZE_TFT );
					}

					LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT *2,
										gSld_sc_Info.ys,LCD_YSIZE_TFT,LCD_XSIZE_TFT,
										gSld_sc_Info.ys,0,gSld_sc_Info.ye - gSld_sc_Info.ys + 1,LCD_YSIZE_TFT );


					GeslastXY = 0;
					Gespos = LCD_YSIZE_TFT;

				}
				else
				{
					for (i = 0; i < LCD_YSIZE_TFT - last_Gespos; i += 4)
					{
						LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT *2,
											gSld_sc_Info.ys,last_Gespos + i,LCD_XSIZE_TFT ,
											gSld_sc_Info.ys,0,gSld_sc_Info.ye - gSld_sc_Info.ys + 1,LCD_YSIZE_TFT );
					}
					LT_CSS_PSRAM_to_TFT(LCD_XSIZE_TFT * LCD_YSIZE_TFT * 2,
										gSld_sc_Info.ys,LCD_YSIZE_TFT,LCD_XSIZE_TFT ,
										gSld_sc_Info.ys,0,gSld_sc_Info.ye - gSld_sc_Info.ys + 1,LCD_YSIZE_TFT );

					GeslastXY = 0;
					Gespos = LCD_YSIZE_TFT;

				}
			}
		}
	}
	else if (GeslastXY != 0)
		GeslastXY = 0;





}
